<?php

/**
 * Description of Dao
 * @package Core
 * @author superx
 */
abstract class Core_Dao extends Core_Singleton {

    protected $_tableClass;
    protected $_modelClass;
    protected $_identity;

    protected function __construct() {
        parent::__construct();
        $class = get_called_class();
        $this->_identity = $class;
        $this->_tableClass = str_replace('Dao_', 'Table_', $class);
        $this->_modelClass = str_replace('Dao_', 'Model_', $class);
    }

    public function getIdentity() {
        return $this->_identity;
    }

    /**
     * @return Core_Db_Table
     */
    public function getTable() {
        $class = $this->_tableClass;
        return $class::getInstance();
    }

    public function getPK() {
        return $this->getTable()->getPK();
    }

    /**
     * @return SelectQuery
     */
    public function getQuery() {
        return $this->getTable()->getQuery();
    }

    public function find($pk) {
        return $this->getTable()->find($pk);
    }

    public function findAll(array $pks) {
        return $this->getTable()->findAll($pks);
    }

    public function findAllBatch(array $pks) {
        return $this->getTable()->findAllBatch($pks);
    }

    public function insert($data) {
        return $this->getTable()->insert($data);
    }

    public function insertBatch(array $data) {
        return $this->getTable()->insertBatch($data);
    }

    public function update($data, $pk) {
        return $this->getTable()->update($data, $pk);
    }

    public function delete($pk) {
        return $this->getTable()->delete($pk);
    }

}
